// @ts-ignore
// import Home from '../src/App.svelte';
// @ts-ignore
// import User from '../view/user.svelte';
// @ts-ignore
// import Chat from '../view/chat.svelte';
// @ts-ignore
// import user from "../view/user.svelte";
import Router, {push} from 'svelte-spa-router'
import { wrap } from "svelte-spa-router/wrap";

import {logined, setlogin, setuser, thisuser} from "../src/main.js";
// import {construct_svelte_component} from "svelte/internal";
// import Component from "svelte/types/compiler/compile/Component.js";

const modules = import.meta.glob('../module/**/*.js',{eager:true})
// console.log("module")
// console.log(modules)

let routesModule={}
for(const path in modules){

    routesModule = Object.assign(modules[path].default,routesModule)

    // console.log(modules[path])

    // console.log(path)
    // modules[path]().then((mod) => {
    //     console.log(path, mod)
    // })
    // construct_svelte_component()
}
// console.log(routesModule)

const routes = {
    // '/': Home,
    // '/home': Home,
    // '/user':User,
    // '/chat':Chat
    '/':wrap({
        asyncComponent:()=>import('../view/login.svelte')}
    ),
    '/homePage':wrap({
        asyncComponent:()=>import('../view/homePage.svelte'),
        conditions: [
            // First pre-condition function
            async (detail) => {
                await fetch("/api/islogin",{

                }).then((res)=>{
                    return res.json()
                }).then((res)=>{
                    if(res.status===0) {
                        setuser(res.account)
                        setlogin()
                    }
                })
                console.log(thisuser)
                if(logined===true&&logined!==undefined){
                    return true;
                }else{
                    alert("请先登录")
                    push('/')
                    return false;
                }
            },
        ]
    }),
    // '/homePage/chat':wrap({
    //     asyncComponent:()=>import('../view/user.svelte')}
    // ),
    '/chat':wrap({
        asyncComponent:()=>import('../view/user.svelte'),
        conditions: [
            // First pre-condition function
            async (detail) => {
                await fetch("/api/islogin",{

                }).then((res)=>{
                    return res.json()
                }).then((res)=>{
                    if(res.status===0) {
                        setuser(res.account)
                        setlogin()
                    }
                })
                console.log(thisuser)
                if(logined===true&&logined!==undefined){
                    return true;
                }else{
                    alert("请先登录")
                    push('/')
                    return false;
                }
            },
        ]
    }),
    ...routesModule
}

export default routes;